package com.yeyoo.websocket.config;

import com.yeyoo.websocket.handler.TestWebSocketHandler;
import com.yeyoo.websocket.handler.YeyooWebSocketInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * Created by SunJianKang@163.com on 2020-11-04.
 *
 * 实现WebSocketConfigurer接口，重写registerWebSocketHandlers方法，
 * 这是一个核心实现方法，配置websocket入口，允许访问的域、注册Handler、SockJs支持和拦截器。
 *
 *
 *
 */
@Configuration
@EnableWebSocket
public class TestWebSockectConfig implements WebSocketConfigurer {


    @Resource
    private TestWebSocketHandler testWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        /**
         * addInterceptors，顾名思义就是为handler添加拦截器，可以在调用handler前后加入我们自己的逻辑代码。
         */
        registry.addHandler(testWebSocketHandler, "/websocket.do")
                .setAllowedOrigins("*")
                .addInterceptors(new YeyooWebSocketInterceptor());


        registry.addHandler(testWebSocketHandler, "/websocket.do")
                .setAllowedOrigins("*")
                .addInterceptors(new YeyooWebSocketInterceptor())
                .withSockJS();
    }
}
